package com.extension.company.view.my.cerrification;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import com.extension.company.admin.R;
import com.extension.company.common.camera.openCameraUtils;
import com.extension.company.common.mvp.BaseTitleActivity;
import com.extension.company.common.permission.PermissionsUtil;
import com.extension.company.Constant;
import com.extension.company.contract.Certification2Dontract;
import com.extension.company.presenter.my.Certification2Presenter;
import com.extension.company.common.widget.popuwindow.PwTakePhoto;
import com.wildma.idcardcamera.camera.CameraActivity;

import java.io.FileNotFoundException;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 拍照进行认证
 */
public class IDPhotoCertificationActivity extends BaseTitleActivity<Certification2Dontract.View, Certification2Presenter>
		implements Certification2Dontract.View {

	@BindView(R.id.btn_submit)
	Button btn_submit;
	@BindView(R.id.front_take_photo)
	ImageView frontTakePhoto;
	@BindView(R.id.back_take_photo)
	ImageView backTakePhoto;
	@BindView(R.id.people_card_take_photo)
	ImageView peopleCardTakePhoto;
	private PwTakePhoto popwindowTakePhoto;

	private final static int fontCard = 0x001;
	private final static int backCard = 0x002;
	private final static int holdCard = 0x003;
	private final static int BODY_PICTURE = 0X004;
	private Uri mImageUri;
	private int cardType;

	private String fontImgUrl, backImgUrl;

	private static final int ID_REQUEST_PERMISSION_CAMER = 12;


	public static void startActivity(Activity activity) {
		Intent intent = new Intent(activity, IDPhotoCertificationActivity.class);
		activity.startActivity(intent);
	}

	@Override
	protected void initData() {
		setTitle("实名认证");
		popwindowTakePhoto = new PwTakePhoto(this);

	}

	@Override
	protected void initListener() {

		popwindowTakePhoto.setBtnOnclickListener(new PwTakePhoto.OnBtnOnClickListener() {
			@Override
			public void cancel() {

			}

			@Override
			public void openCanmer() {
				switch (cardType) {
					case fontCard:
						CameraActivity.toCameraActivity(IDPhotoCertificationActivity.this, CameraActivity.TYPE_IDCARD_FRONT);
						popwindowTakePhoto.dismiss();
						break;
					case backCard:
						CameraActivity.toCameraActivity(IDPhotoCertificationActivity.this, CameraActivity.TYPE_IDCARD_BACK);
						popwindowTakePhoto.dismiss();
						break;
					case holdCard:
						mImageUri = openCameraUtils.startActivityForCamera(IDPhotoCertificationActivity.this);
						popwindowTakePhoto.dismiss();
						break;
					default:
						break;
				}

			}

			@Override
			public void openGallerys() { //打开相册
				Intent intentToPickPic = new Intent(Intent.ACTION_PICK, null);
				intentToPickPic.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/jpeg");
				startActivityForResult(intentToPickPic, Constant.SELECT_REQUEST_GALLERY);
				popwindowTakePhoto.dismiss();
			}
		});

	}

	@Override
	public int getMainLayout() {
		return R.layout.my_certification2;
	}

	@Override
	public Certification2Dontract.View createView() {
		return this;
	}

	@Override
	public Certification2Presenter createPresenter() {
		return new Certification2Presenter();
	}

	@Override
	public void showLoading() {

	}

	@Override
	public void hideLoading() {

	}

	@Override
	public void onError(Throwable throwable) {

	}

	@OnClick({R.id.front_take_photo, R.id.back_take_photo, R.id.people_card_take_photo, R.id.btn_submit})
	public void onViewClicked(View view) {
		switch (view.getId()) {
			case R.id.front_take_photo:
				popwindowTakePhoto.show();
				cardType = 0x001;
				break;
			case R.id.back_take_photo:
				popwindowTakePhoto.show();
				cardType = 0x002;
				break;
			case R.id.people_card_take_photo:
				boolean a = PermissionsUtil.openPermissions(IDPhotoCertificationActivity.this, PermissionsUtil.OPEN_CAMER_PERMISSIONS, ID_REQUEST_PERMISSION_CAMER);
				if (a) {
					popwindowTakePhoto.show();
				}
				cardType = 0x003;
				break;
			case R.id.btn_submit:
				CertificationFaileActivity.startActivity(this);
				break;
			default:
				break;
		}
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
		if (resultCode == RESULT_OK) {
			switch (requestCode) {
				case Constant.PHOTO_REQUEST_CAMERA:   // 手持身份的回调
					Bitmap bitmap = null;
					try {
						bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(mImageUri));
						Log.d("zwk", mImageUri.getPath()); // 绝对路径
//						openCameraUtils.getImagePath(this, mImageUri);
						peopleCardTakePhoto.setImageBitmap(bitmap);
					} catch (FileNotFoundException e) {
						e.printStackTrace();
					}
					break;
				case Constant.SELECT_REQUEST_GALLERY:
					ContentResolver cr = this.getContentResolver();
					Uri uri = data.getData();
					Log.d("zwk", uri.getPath());
					String realPath = openCameraUtils.getImagePath(this, uri);
					Log.d("zwk", "真实路径" + realPath);
					switch (cardType) {
						case fontCard:
							try {
								frontTakePhoto.setImageBitmap(BitmapFactory.decodeStream(cr.openInputStream(uri)));
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}

							break;
						case backCard:
							try {
								backTakePhoto.setImageBitmap(BitmapFactory.decodeStream(cr.openInputStream(uri)));
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
							break;
						case holdCard:
							try {
								peopleCardTakePhoto.setImageBitmap(BitmapFactory.decodeStream(cr.openInputStream(uri)));
							} catch (FileNotFoundException e) {
								e.printStackTrace();
							}
							break;
					}
					break;
				default:
					break;

			}
		} else if (requestCode == CameraActivity.REQUEST_CODE && resultCode == CameraActivity.RESULT_CODE) {  // 身份证正反面的图片的回调
			String path = CameraActivity.getImagePath(data);
			Log.d("zwk", path);
			if (cardType == fontCard) {
				if (!TextUtils.isEmpty(path)) {
					fontImgUrl = path;
					frontTakePhoto.setImageBitmap(BitmapFactory.decodeFile(path));
				}

			} else if (cardType == backCard) {
				if (!TextUtils.isEmpty(path)) {
					backImgUrl = path;
					backTakePhoto.setImageBitmap(BitmapFactory.decodeFile(path));
				}
			}
		}
	}


}
